<template>
    <div>
        <el-form ref="ruleFormRef" :model="quesData" :rules="rules" class="question-info">
            <div>
                <div class="info1">
                    <div class="title">
                        <span>题干</span>
                        <el-form-item prop="questionTitle">
                            <el-input v-model="quesData.questionTitle" type="textarea" placeholder="题干，点此编辑（必填）"
                                class="input" />
                        </el-form-item>

                    </div>
                    <el-divider border-style="dashed" />
                </div>
            </div>
            <div class="info2">
                <div class="xuanxiang">
                    <div class="spanBoder">
                        <span class="words">选项</span> 
                    </div>
                    <el-form-item prop="questionAnswer" class="radio">
                        <el-radio-group v-model="quesData.questionAnswer">
                            <el-radio :label="item.value" v-for="item in radios">{{ item.label }}</el-radio>
                        </el-radio-group>
                    </el-form-item>
                </div>
            </div>
            <div>
                <el-divider border-style="dashed" />
                <div class="info3">
                    <span>解析</span>
                    <el-form-item prop="description">
                        <el-input v-model="quesData.description" type="textarea" placeholder="题目解析，点此编辑（选填）"
                            class="input" />
                    </el-form-item>
                </div>
            </div>
        </el-form>
    </div>
</template>

<script setup>
import { ref, reactive, defineExpose, onMounted, watch } from 'vue'
import { useRoute } from 'vue-router'
import { getqueryBankAPI } from '@/api/api'

const route = useRoute()

const props = defineProps({
    childData: {
        default: {}
    },
    questionId: {
        default: "",
    }
})

//题目内容信息
const quesData = ref({
    //题目
    questionTitle: '',
    //答案
    questionAnswer: '',
    //解析
    description: '',
})
// 单选
const radios = ref([
    {
        label: '正确',
        value: true,
    },
    {
        label: '错误',
        value: false,
    }
])

const ruleFormRef = ref(null)
const rules = reactive({
    questionTitle: [
        { required: true, message: '请填写题目内容', trigger: 'blur' },
    ],
    description: [
        { required: true, message: '请填写解析，若无请写‘无数据’', trigger: 'blur' },
    ],
    questionAnswer: [
        { required: true, message: "请至少选择一个选项", trigger: 'blur' },
    ],
})

onMounted(() => {
    quesData.value = props.childData
    if (props.questionId) {
        //查询题目
        getqueryBankAPI(props.questionId).then(res => {
            if (res.data.code == 200) {
                res.data.data.questionAnswer = JSON.parse(res.data.data.questionAnswer)
                quesData.value = res.data.data
                console.log(res.data.data,'data填空题');
            }
        })
    }
})

//watch侦听getter函数  () =>
watch(() => quesData.value, (value) => {
    emit('update:childData', value)

}, { deep: true })
//将值传给父组件
const addData = ref([])
const emit = defineEmits(['tureFalseChild-click', 'update:childData'])
const tureFalseData = () => {
    submit()
    addData.value.push({
        questionTitle: quesData.value.questionTitle,
        questionAnswer: JSON.parse(quesData.value.questionAnswer),
        description: quesData.value.description,
    })
    console.log('父组件触发子组件了');
    emit('tureFalseChild-click', addData.value);
}
//重置数据
const tureFalseClearData = () => {
    quesData.value.questionTitle = ''
    quesData.value.questionAnswer = ''
    quesData.value.description = ''
}
defineExpose({
    tureFalseData, tureFalseClearData
})
//提交表单验证
const submit = () => {
    ruleFormRef.value.validate((valid) => {
        if (valid) {
            console.log("通过");
        } else {
            console.log("未通过");
        }
    });
};

</script>

<style lang="less" scoped>
.info1 {
    .title {
        margin-top: 25px;
        display: flex;


        span {
            font-size: small;
            width: 30px;
            height: 20px;
            margin: 15px 20px;
            background-color: #E5EAF3;
            padding: 0 15px;
        }

        .input {
            width: 800px;

        }
    }

}

.info2 {
    .xuanxiang {
        display: flex;
        margin-left: 10px;

        .spanBoder {
            display: flex;

            .words {
                font-size: small;
                width: 30px !important;
                height: 20px;
                margin: 15px;
                background-color: #E5EAF3;
                padding: 0 16px;
            }
        }

        .radio {
            margin: 2px 0 0 10px;
        }
    }
}

.info3 {
    display: flex;
    margin-bottom: 15px;


    span {
        font-size: small;
        width: 30px;
        height: 20px;
        margin: 15px 20px;
        background-color: #E5EAF3;
        padding: 0 15px;
    }

    .input {
        width: 800px;

    }
}

/deep/ .el-radio__input.is-checked .el-radio__inner::after {
    content: '';
    width: 8px;
    height: 3px;
    border: 2px solid white;
    border-top: transparent;
    border-right: transparent;
    text-align: center;
    display: block;
    position: absolute;
    top: 2px;
    left: 1px;
    transform: rotate(-45deg);
    border-radius: 0px;
    background: none;
}
</style>